import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter_chubao/config/app_config.dart';
import 'package:flutter_chubao/tools/tool_app/app_dio_response.dart';
import 'package:flutter_chubao/tools/tool_app/app_dio_transformer.dart';

import 'app_dio_service.dart';
import 'app_dio_config.dart';
import 'app_dio_parse.dart';

class AppDioClient {
  static late AppDioService _dio;
  static String appToken = '';
  static int timeDifference = 0;
  // static String version = '';
  // static String platform = '';
  // static String pagename = 'liveChat';

  factory AppDioClient() => _getInstance();
  static AppDioClient get instance => _getInstance();
  static AppDioClient? _instance;
  static String httpEncryption = 'q#fAn[kQ%%bM';
  AppDioClient._init() {
    // 初始化
  }
  static AppDioClient _getInstance() {
    _instance ??= AppDioClient._init();
    return _instance!;
  }

  static initDio({BaseOptions? options, AppDioConfig? dioConfig}) {
    _dio = AppDioService(options: options, dioConfig: dioConfig);
    _dio.interceptors.add(InterceptorsWrapper(
      onRequest: (options, handler) async {
        // SSConfigApp.packageInfo?.version

        // String token = SSAppDioClient.appToken;
        // Map data = {
        //   if (options.data != null) ...options.data,
        //   "userId": await SSUtilShared.getString(SSUtilConstant.userId),
        //   "channelCode": await SSUtilShared.getString(SSUtilConstant.channel),
        // };
        options.headers = {
          ...options.headers,
          // "channel-id": AppConfig.channelId,
          // "device-id": AppConfig.deviceId,
          "Authorization": AppDioClient.appToken
        };

        return handler.next(options);
      },
    ));
    // PackageInfo packageInfo = await PackageInfo.fromPlatform();
    // version = '1.0.0';
    // platform = Platform.isAndroid ? '0' : '1';
    //    static String version = '';
    // static String platform = '';
  }

  static Future<AppDioResponse> get(String uri,
      {Map<String, dynamic>? queryParameters,
      Options? options,
      CancelToken? cancelToken,
      ProgressCallback? onReceiveProgress,
      AppDioTransformer? httpTransformer}) async {
    try {
      var response = await _dio.get(
        uri,
        queryParameters: queryParameters,
        options: options,
        cancelToken: cancelToken,
        onReceiveProgress: onReceiveProgress,
      );
      return handleResponse(response, httpTransformer: httpTransformer);
    } on Exception catch (e) {
      return handleException(e);
    }
  }

  static Future<AppDioResponse> post(String uri,
      {Map<String, dynamic>? data,
      Map<String, dynamic>? queryParameters,
      Options? options,
      CancelToken? cancelToken,
      ProgressCallback? onSendProgress,
      ProgressCallback? onReceiveProgress,
      AppDioTransformer? httpTransformer}) async {
    try {
      var response = await _dio.post(
        uri,
        data: data,
        queryParameters: queryParameters,
        options: options,
        cancelToken: cancelToken,
        onSendProgress: onSendProgress,
        onReceiveProgress: onReceiveProgress,
      );
      return handleResponse(response, httpTransformer: httpTransformer);
    } on Exception catch (e) {
      return handleException(e);
    }
  }

  static Future<AppDioResponse> upload(String uri,
      {required File imageFile,
      // Map<String, dynamic>? data,
      Map<String, dynamic>? queryParameters,
      ProgressCallback? onSendProgress,
      ProgressCallback? onReceiveProgress,
      AppDioTransformer? httpTransformer}) async {
    try {
      String path = imageFile.path;
      var name = path.substring(path.lastIndexOf("/") + 1, path.length);
      FormData formdata = FormData.fromMap(
          {"file": await MultipartFile.fromFile(path, filename: name)});
      var response = await _dio.post(
        uri,
        data: formdata,
        queryParameters: queryParameters,
        // options: options,
        // cancelToken: cancelToken,
        onSendProgress: onSendProgress,
        onReceiveProgress: onReceiveProgress,
      );
      return handleResponse(response, httpTransformer: httpTransformer);
    } on Exception catch (e) {
      return handleException(e);
    }
  }

  static Future<AppDioResponse> patch(String uri,
      {data,
      Map<String, dynamic>? queryParameters,
      Options? options,
      CancelToken? cancelToken,
      ProgressCallback? onSendProgress,
      ProgressCallback? onReceiveProgress,
      AppDioTransformer? httpTransformer}) async {
    try {
      var response = await _dio.patch(
        uri,
        data: data,
        queryParameters: queryParameters,
        options: options,
        cancelToken: cancelToken,
        onSendProgress: onSendProgress,
        onReceiveProgress: onReceiveProgress,
      );
      return handleResponse(response, httpTransformer: httpTransformer);
    } on Exception catch (e) {
      return handleException(e);
    }
  }

  static Future<AppDioResponse> delete(String uri,
      {data,
      Map<String, dynamic>? queryParameters,
      Options? options,
      CancelToken? cancelToken,
      AppDioTransformer? httpTransformer}) async {
    try {
      var response = await _dio.delete(
        uri,
        data: data,
        queryParameters: queryParameters,
        options: options,
        cancelToken: cancelToken,
      );
      return handleResponse(response, httpTransformer: httpTransformer);
    } on Exception catch (e) {
      return handleException(e);
    }
  }

  static Future<AppDioResponse> put(String uri,
      {data,
      Map<String, dynamic>? queryParameters,
      Options? options,
      CancelToken? cancelToken,
      AppDioTransformer? httpTransformer}) async {
    try {
      var response = await _dio.put(
        uri,
        data: data,
        queryParameters: queryParameters,
        options: options,
        cancelToken: cancelToken,
      );
      return handleResponse(response, httpTransformer: httpTransformer);
    } on Exception catch (e) {
      return handleException(e);
    }
  }

  static Future<Response> download(String urlPath, savePath,
      {ProgressCallback? onReceiveProgress,
      Map<String, dynamic>? queryParameters,
      CancelToken? cancelToken,
      bool deleteOnError = true,
      String lengthHeader = Headers.contentLengthHeader,
      data,
      Options? options,
      AppDioTransformer? httpTransformer}) async {
    try {
      var response = await _dio.download(
        urlPath,
        savePath,
        onReceiveProgress: onReceiveProgress,
        queryParameters: queryParameters,
        cancelToken: cancelToken,
        deleteOnError: deleteOnError,
        lengthHeader: lengthHeader,
        data: data,
        options: data,
      );
      return response;
    } catch (e) {
      rethrow;
    }
  }
}
